<?php
// 本类由系统自动生成，仅供测试用途
class TplAction extends BaseAction {
	
	function _before_index() {
		if( empty($_REQUEST['tpl_type'] ))
		{			
			$_REQUEST['tpl_type'] = 'base';
		}
	}

	function update() {

		$dao = D ( 'Tpl' );

		if (false === $dao->create ()) {
			$this->error ( $dao->getError () );
		}
		$id = (int)$_REQUEST['id'];
		if( empty( $id ) )
			$this->error('参数Id不能为空！');

		// 更新数据
		$list=$dao->save ();
		if (false !== $list) {
			//生成标签模板
			$file = C('TPL_PATH').$id.'.html';
			$tagTpl = $dao->find($id);
			file_put_contents($file,$tagTpl['content']);

			//成功提示
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('编辑成功!');
		} else {
			//错误提示
			$this->error ('编辑失败!');
		}
	}

	function insert() {    	$dao = D ( 'Tpl' );

		if (false === $dao->create ()) {
			$this->error ( $dao->getError () );
		}


    	$list=$dao->add ();
		if ($list!==false) { 	//保存成功

			$id = $list;
			//生成标签模板
			$file = C('TPL_PATH').$id.'.html';
			$tagTpl = $dao->find($id);
			file_put_contents($file,$tagTpl['content']);

			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('新增成功!');
		}
		 else {	//失败提示
			$this->error ('新增失败!');
		}

	}


	public function del()
	{
		$ids = $_REQUEST['id'];
		if( empty( $ids ) )
			$this->error('参数Id:不能为空！');
		if( is_array( $ids ) )
		{			foreach($ids as $id )
			{             	if( !$this->del_one($id) )
             	{             		$this->error('删除失败(Id:'.$id.')！');             	}

			}
			$this->success('删除成功！');
		}
		else
		{			$id = (int) $ids;
   			if( !$this->del_one($id) )
           	{
           		$this->error('删除失败(Id:'.$id.')！');
           	}
		}

	}

	private function del_one($id)
	{
		$dao = D ( 'Tpl' );
		if (! empty ( $dao )) {
			$t = $dao->find($id);
			if( $t['is_sys'] == 1 )
			{
			 	return false;
			}        	$rt = $dao->where ( "id=$id" )->delete();
        	if( $rt !== FALSE)
        	{        		//删除标签模板
				$file = C('TPL_PATH').$id.'.html';
    			unlink($file);
    			return true;
        	}
        	else
        		return false;
		}
		else
			return false;	}

	/**
	   *  更新模板
	   */
	public function  update_tpl()
	{
		$this->display();
	}


	/**
	   *  生成模板文件
	   *
	   */
	public function create_tpl()
	{
   		$dao = D ( 'Tpl' );
   		$tpl_list = $dao->findAll();
   		$cnt = count($tpl_list);
   		for($i=0;$i<$cnt;$i++)
   		{
   			$id = $tpl_list[$i]['id'];
         	$content = $tpl_list[$i]['content'];
         	$tpl_name = $tpl_list[$i]['title'];
         	$file = C('TPL_PATH').$id.'.html';
			file_put_contents($file,$content);
			echo '生成模板：'.$tpl_name.'<br />';
   		}

   		echo '更新成功！';
   		//echo '<script language="javascript">window.setTimeouthistory.back();</script>';	//返回
   		echo '<meta http-equiv="Refresh" content="1;URL='.__URL__.'/update_tpl">';
	}

	/**
       *  清除模板缓存
       *
       */
 	public function del_cache()
 	{
 		import('ORG.Io.Dir');
 		$cache_path = C('WEB_CACHE_PATH');
      	$dir = new Dir($cache_path,'');
		$dir->del($cache_path);
		$this->success ('操作成功!');

 	}
}
?>